package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.TfyclientFormDao;
import cn.gson.fengboot.model.dao.TfylinkmanFormDao;
import cn.gson.fengboot.model.dao.TfyrelationwarnDao;
import cn.gson.fengboot.model.mapper.TfyrelationwarnMapper;
import cn.gson.fengboot.model.pojos.QempFrom;
import cn.gson.fengboot.model.pojos.TfylinkmanForm;
import cn.gson.fengboot.model.pojos.TfyrelationwarnFrom;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TfyrelationwarnService {
    @Autowired
    TfyrelationwarnDao tfyrelationwarnDao;//联系提醒

    @Autowired
    TfyclientFormDao tfyclientFormDao;//客户

    @Autowired
    TfylinkmanFormDao tfylinkmanFormDao;//联系人

    @Autowired
    TfyrelationwarnMapper tfyrelationwarnMapper;//联系提醒mapper

    //新增联系提醒方法
    public void addrelationwarn(TfyrelationwarnFrom lxtx){
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = df.format(new Date());
//        Timestamp ts = Timestamp.valueOf(time);
        lxtx.setClueId(null);
        //lxtx.setRelationwarnJd(lxtx.getRelationwarnZt());
//        else if (lxtx.getRelationwarnTime().before(ts)){
////            lxtx.setRelationwarnJd(2);
////        }
        tfyrelationwarnDao.save(lxtx);
    }

    //根据客户id查询联系提醒信息
    public List<TfyrelationwarnFrom> cxidrelationwarn(Integer clientId){
        return tfyrelationwarnMapper.cxidrelationwarn(clientId);
    }

    //根据客户id查询已完成联系提醒信息
    public List<TfyrelationwarnFrom> wcidrelationwarn(Integer clientId){
        return tfyrelationwarnMapper.wcidrelationwarn(clientId);
    }

    //根据客户id查询已延期联系提醒信息
    public List<TfyrelationwarnFrom> yqidrelationwarn(Integer clientId){
        return tfyrelationwarnMapper.yqidrelationwarn(clientId);
    }

    //根据联系人id查询联系提醒信息
    public List<TfyrelationwarnFrom> lxridrelationwarn(Integer linkmanId){
        return tfyrelationwarnMapper.lxridrelationwarn(linkmanId);
    }

    //根据联系人id查询已完成联系提醒信息
    public List<TfyrelationwarnFrom> lxrwcidrelationwarn(Integer linkmanId){
        return tfyrelationwarnMapper.lxrwcidrelationwarn(linkmanId);
    }

    //根据联系人id查询已延期联系提醒信息
    public List<TfyrelationwarnFrom> lxryqidrelationwarn(Integer linkmanId){
        return tfyrelationwarnMapper.lxryqidrelationwarn(linkmanId);
    }

    //根据员工id查询今日联系提醒信息
    public List<TfyrelationwarnFrom> jridrelationwarn(Integer empId){
        return tfyrelationwarnMapper.jridrelationwarn(empId);
    }

    //根据员工id查询明日联系提醒信息
    public List<TfyrelationwarnFrom> mridrelationwarn(Integer empId){
        return tfyrelationwarnMapper.mridrelationwarn(empId);
    }

    //根据员工id查询即将联系提醒信息
    public List<TfyrelationwarnFrom> jjidrelationwarn(Integer empId){
        return tfyrelationwarnMapper.jjidrelationwarn(empId);
    }

    //根据员工id查询已延期联系提醒信息
    public List<TfyrelationwarnFrom> yyqidrelationwarn(Integer empId){
        return tfyrelationwarnMapper.yyqidrelationwarn(empId);
    }

    //修改联系提醒状态方法
    public void xgztrelationwarn(TfyrelationwarnFrom lxtx){
        lxtx.setRelationwarnZt(1);
        tfyrelationwarnDao.save(lxtx);
    }

    //修改联系提醒状态0方法
    public void xgztwrelationwarn(TfyrelationwarnFrom lxtx){
        lxtx.setRelationwarnZt(0);
        tfyrelationwarnDao.save(lxtx);
    }

    //联系提醒物理删除
    public void sclxtx(TfyrelationwarnFrom lxtx){
        tfyrelationwarnDao.delete(lxtx);
    }

    //客户合并联系提醒
    public List<TfyrelationwarnFrom> khhbrelationwarn(Integer id1, Integer id2){
        return tfyrelationwarnMapper.khhbrelationwarn(id1,id2);
    }

    //联系提醒批量物理删除
    public void deleteRelationwarn(String khdx){
        JSONObject obj = (JSONObject)JSONObject.parse(khdx);
        List khdxlist = obj.getObject("khdxlist", List.class);
        //转化成JSONArray
        JSONObject object=null;
        JSONArray json = JSONArray.parseArray(String.valueOf(khdxlist));
        System.out.println("jihe：："+khdxlist.toString());
        for (int i=0; i<json.size();i++) {
            object = (JSONObject) json.get(i);//得到对象
            Integer khdxs = object.getInteger("relationwarnId");
            System.out.println("联系提醒ID：" + khdxs);
            System.out.println("siowjj " + obj.toString());
            TfyrelationwarnFrom tfyrelationwarnFrom = tfyrelationwarnDao.findById(khdxs).get();
            System.out.println("客户：" + tfyrelationwarnFrom.toString());
            tfyrelationwarnDao.delete(tfyrelationwarnFrom);
        }
    }

    //根据联系人id查询联系提醒
    public List<TfyrelationwarnFrom> lxrlxtx(int linkmanId){
        return tfyrelationwarnMapper.lxrlxtx(linkmanId);
    }
}
